﻿using Common.SqlSugar;
using MyBlog.IService;
using MyBlog.Model.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.Service
{
    public class BlogService : BaseService<Blog>, IBlogService
    {
        public BlogService(ISqlSugarClient db) : base(db)
        {
        }

        public async Task<List<Blog>> QueryOrderbyMaxAccess(Expression<Func<Blog, bool>> expression, int page, int size, RefAsync<int> total, bool order = true)
        {
            if (order)
                return await Context.Queryable<Blog>().Where(expression).OrderBy(x => x.Views).ToPageListAsync(page, size, total);
            else
                return await Context.Queryable<Blog>().Where(expression).OrderByDescending(x => x.Views).ToPageListAsync(page, size, total);
        }

        public async Task<List<Blog>> QueryOrderbyTime(Expression<Func<Blog, bool>> expression, int page, int size, RefAsync<int> total,bool order=true)
        {
            if(order)
                return await Context.Queryable<Blog>().Where(expression).OrderBy(x=>x.CreateTime).ToPageListAsync(page, size, total);
            
                return await Context.Queryable<Blog>().Where(expression).OrderByDescending(x => x.CreateTime).ToPageListAsync(page, size, total);
        }

    }
}
